package be.kuleuven.mume.rest;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONObject;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import be.kuleuven.mume.JSONHelper;
import be.kuleuven.mume.PMF;
import be.kuleuven.mume.beans.PersoonBean;
import be.kuleuven.mume.beans.VakBean;
import be.kuleuven.mume.query.VakQuery;

@Path("/vak")
public class Vak {

	/**
	 * 
	 */
	private static final Logger log = Logger.getLogger(Vak.class.getName());
	private static final long serialVersionUID = -3291252979009718051L;
	
	@GET
	@Path("/")
	@Produces ("application/json")
	public String doGet(@QueryParam("rpp")@DefaultValue("10") int rpp,
			@QueryParam("page")@DefaultValue("0") int page) {
		
		try{
			
			/*
			Todo:
			PersoonBean p = PersoonBean.getCurrentPersoon();
				return "{\"error\": \"User Not logged in\"}";*/
			
			PersistenceManager pm = PMF.getPersistenceManager();
			Query query = pm.newQuery(VakBean.class);
			query.setRange(page*rpp, (page+1)*rpp);
			query.setOrdering("name ascending");
			@SuppressWarnings("unchecked")
			List<VakBean> vakken = (List<VakBean>)query.execute();
			
			JSONObject result = JSONHelper.convertToJSONResult(vakken);
			
			pm.close();
			
			return result.toString();
			
		} catch (Exception e){
			log.log(Level.SEVERE,"Exception",e);
		} /*catch (UserNotLoggedInException e) {
			return e.toJsonString();
		}*/
		
		return JSONHelper.ERROR;
	}
	
	@GET
	@Path("/{vakid}")
	@Produces ("application/json")
	public String doGet(@PathParam("vakid")String vakid) {
		
		try{
			
			PersistenceManager pm = PMF.getPersistenceManager();
			Key key = KeyFactory.stringToKey(vakid);
			VakBean vak = pm.getObjectById(VakBean.class, key);
			JSONObject result = JSONHelper.convertToJSONResult(new JSONObject(vak));
			pm.close();
			
			return result.toString();
			
		} catch (Exception e){
			log.log(Level.SEVERE,"Exception",e);
		} /*catch (UserNotLoggedInException e) {
			return e.toJsonString();
		}*/
		
		return JSONHelper.ERROR;
	}

	@GET
	@Path("/delete/{vakid}")
	@Produces ("application/json")
	public String deleteVak(@PathParam("vakid") String vakId) {
		try {
			VakBean.delete(vakId);
		
			return JSONHelper.SUCCES;
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		
		return JSONHelper.ERROR;
	}
	
	@POST
	@Path("/update")
    @Consumes("application/json")
	@Produces ("application/json")
	public String updateVak(String vakJson) {
		
		try {
			JSONObject object = new JSONObject(vakJson);
			VakBean vak = VakQuery.getVak(object.getString("vakId"));
			vak.setName(object.getString("name"));
			vak.setHashTag(object.getString("hashTag"));
			
			vak.store();
			
			return JSONHelper.SUCCES;
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}

	@GET
	@Path("/add/{vakname}/{profid}")
	@Produces ("application/json")
	public String addVak(@PathParam("vakname") String name, 
			@PathParam("profid")String profId, @QueryParam("hashtag") String hashtag) {
		try{
	
			VakBean vak = new VakBean();
	
			vak.setName(name);
			vak.setHashTag(hashtag);
			vak.setProf(profId);
			//Store new Vak to the db
			vak.store();

			return "{\"result\": \"succes\",\"vakid\":\""+ vak.getVakId() + "\"}";
			
		} catch (Exception e) {
			log.log(Level.SEVERE, "Exception", e);
		}
		return JSONHelper.ERROR;
	}
	
	@GET
	@Path("/user/{vakId}")
	@Produces ("application/json")
	public String getUsers(@PathParam("vakId")String vakId, 
			@QueryParam("rpp")@DefaultValue("10")int rpp,
			@QueryParam("page")@DefaultValue("0")int page) {
		
		try{
			List<PersoonBean> personen = VakQuery.getVakStudenten(vakId, page, rpp);
			return JSONHelper.convertToJSONResult(personen).toString();
			
		} catch (Exception e){
			log.log(Level.SEVERE,"Exception",e);
		}
		return JSONHelper.ERROR;
	}
}
